package com.taxshare.purchaser.service.rulesReview;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.entity.InvoiceDetailEntity;
import com.taxshare.purchaser.entity.InvoiceGoodsRulesEntity;
import com.taxshare.purchaser.mapper.InvoiceDetailMapper;
import com.taxshare.purchaser.mapper.InvoiceGoodsRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @ClassName: GoodsReviewCheck
 * @Description: 商品抬头审核规则
 * @author: hjh
 * @date: 2021/7/16 13:38
 */
@Component
public class GoodsReviewCheck implements RuleMainHandler {

    @Autowired
    InvoiceGoodsRulesMapper invoiceGoodsRulesMapper;

    @Autowired
    InvoiceDetailMapper invoiceDetailMapper;

    @Override
    public CheckInfoRespVO checkRule(InvoiceCollectionEntity bean, String pid) {
        InvoiceGoodsRulesEntity info = getInfo(pid);
        CheckInfoRespVO vo = new CheckInfoRespVO();
        //审核规则不为空
        if (info != null) {
            //商品抬头拆分
            if (StrUtil.isNotBlank(info.getGoodsSensitiveHead())) {
                List<String> list = Arrays.stream(info.getGoodsSensitiveHead().split(",")).collect(Collectors.toList());
                List<InvoiceDetailEntity> invoiceDetailEntities = invoiceDetailMapper.selectList(new QueryWrapper<InvoiceDetailEntity>().eq("invoice_code", bean.getInvoiceCode()).eq("invoice_no", bean.getInvoiceNo()));
                List<String> goodsNameList = invoiceDetailEntities.stream().map(item -> item.getGoodsName()).collect(Collectors.toList());
                for (String s : list) {
                    Pattern pattern = Pattern.compile(s);
                    for (String goodsName : goodsNameList) {
                        Matcher matcher = pattern.matcher(goodsName);
                        if (matcher.find()){
                            Boolean flg = checkInvoiceType(info, bean);
                            if (flg) {
                                vo.setGoing(false);
                                vo.setFlag(false);
                                vo.setMsg(info.getGoodsNotPassReasons());
                            }
                        }
                    }
                }
            }
        }
        return vo;
    }

    @Override
    public CheckInfoRespVO checkSubmit(String id) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        InvoiceGoodsRulesEntity info = getInfo(id);
        if (info!=null&&CodeConstant.COMM_CODE_1.equals(info.getForceFlag())){
            vo.setGoing(false);
            vo.setMsg("敏感商品名称规则强制校验不通过!");
        }
        return vo;
    }

    /**
     * 校验发票类型
     * @param info
     * @param bean
     * @return
     */
    private Boolean checkInvoiceType(InvoiceGoodsRulesEntity info, InvoiceCollectionEntity bean) {
        if (CodeConstant.INVOICE_QB.equals(info.getGoodsTicketType())) {
            return Boolean.TRUE;
        }
        if (CodeConstant.INVOICE_PP.equals(info.getGoodsTicketType())) {
            if (CodeConstant.INVOICE_PP_LIST.contains(bean.getInvoiceType())) {
                return Boolean.TRUE;
            }
        }
        if (CodeConstant.INVOICE_ZP.equals(info.getGoodsTicketType())) {
            if (CodeConstant.INVOICE_ZP.equals(bean.getInvoiceType())) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }


    /**
     * 查询规则开启状态
     *
     * @param pid
     * @return 购方规则
     */
    private InvoiceGoodsRulesEntity getInfo(String pid) {
        InvoiceGoodsRulesEntity entity = invoiceGoodsRulesMapper.selectOne(new QueryWrapper<InvoiceGoodsRulesEntity>().eq("uuid", pid));
        if (entity != null && CodeConstant.RULES_OPEN.equals(entity.getGoodsIsOpen())) {
            return entity;
        }
        return null;
    }
}
